package com.dudu.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ddf.EscherColorRef;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class RequestUtil {

	public static String getParams(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		Enumeration enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String paramName = (String) enu.nextElement();
			String paramValue = request.getParameter(paramName);
			try {
				paramValue = new String(paramValue.getBytes("ISO8859-1"),
						"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			sb.append(paramName + ":" + paramValue + ",");
		}
		String str = StringUtils.removeEnd(sb.toString(), ",") + "]";
		return str;
	}

	public static String getParamsMulti(HttpServletRequest request) {
		Map<String,List<String>> paraMap = new HashMap<String,List<String>>();
		Map<String,Object> paraResult = new HashMap<String,Object>();
		String queryString = request.getQueryString().trim();
		if(StringUtils.isNoneBlank(queryString)){
			String[] paramArray = queryString.split("&");
			for(String param : paramArray){
				String[] map = param.split("=");
				String paramName = map[0];
				if(map.length>1){
					String value = map[1];
					if(StringUtils.isNoneBlank(value)){
						try {
							value = new String(value.getBytes("ISO8859-1"),
									"UTF-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						if(paraMap.containsKey(paramName)){
							paraMap.get(paramName).add(value);
						}else{
							List<String> values = new ArrayList<String>();
							values.add(value);
							paraMap.put(paramName, values);
						}
					}
				}
			}
		}
		for(String key : paraMap.keySet()){
			List<String> valueList = paraMap.get(key);
			if(valueList == null){
				continue;
			}
			if(key.indexOf("questionableTypes")>-1){
				String newKey = key.replace("%5B%5D", "");
				paraResult.put(newKey, valueList);
			}else{
				paraResult.put(key, valueList.get(0));
			}
		}
		return JSON.toJSONString(paraResult);
	}
	
	public static String getSessionStr(HttpServletRequest request) {
		StringBuffer sb = new StringBuffer();
		sb.append("[session=][");
		HttpSession session = request.getSession();
		if (session != null) {
		    Enumeration names = session.getAttributeNames();
		    while(names.hasMoreElements()){
		        String name =(String) names.nextElement();
		        sb.append(name).append(":").append(session.getAttribute(name));
		    }
		}
		String str = StringUtils.removeEnd(sb.toString(), ",") + "]";
		return str;
	}

	public static String getPostParameter(HttpServletRequest request) {
		BufferedInputStream buf = null;
		int iContentLen = request.getContentLength();
		byte sContent[] = new byte[iContentLen];
		String sContent2 = null;
		try {
			buf = new BufferedInputStream(request.getInputStream());
			buf.read(sContent, 0, sContent.length);
			sContent2 = new String(sContent, 0, iContentLen, "UTF-8");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				buf.close();
			} catch (IOException e) {

			}
		}
		return sContent2;
	}


	public static void main(String[] args) {
		String str = "00010";

		String id = str.substring(1);
		System.out.println(id);
	}

}
